package com.shuwen.gcdj.controller.dictionary;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shuwen.gcdj.bean.dictionary.request.DictionaryAddParam;
import com.shuwen.gcdj.bean.dictionary.request.DictionaryListParam;
import com.shuwen.gcdj.bean.dictionary.request.DictionaryMainParam;
import com.shuwen.gcdj.bean.dictionary.request.DictionaryUpdateParam;
import com.shuwen.gcdj.bean.dictionary.response.DictionaryDetailVO;
import com.shuwen.gcdj.bean.dictionary.response.DictionaryMainVO;
import com.shuwen.gcdj.common.annotation.Auth;
import com.shuwen.gcdj.common.annotation.JWTAuth;
import com.shuwen.gcdj.common.api.CommonPage;
import com.shuwen.gcdj.common.api.CommonResult;
import com.shuwen.gcdj.common.enums.ErrorCode;
import com.shuwen.gcdj.common.enums.LogModuleEnum;
import com.shuwen.gcdj.common.enums.LogTypeEnum;
import com.shuwen.gcdj.common.sysLog.AddSysLogUtil;
import com.shuwen.gcdj.controller.AuthController;
import com.shuwen.gcdj.dto.account.InitUserDto;
import com.shuwen.gcdj.entity.Dictionary;
import com.shuwen.gcdj.service.dictionary.DictionaryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


/**
 * @oauth psiyi
 * @createDate 2023/03/14
 * @info
 */
@Api(tags = "数据字典")
@Auth
@RestController
@RequestMapping("/dictionary")
@Slf4j
public class DictionaryController  extends AuthController {

    @Autowired
    private DictionaryService dictionaryService;

    @Autowired
    private AddSysLogUtil addSysLogUtil;

    @ApiOperation(value = "数据字典-分页")
    @PostMapping(value = "/main")
    @JWTAuth
    public CommonPage<DictionaryMainVO> getPages(@RequestBody DictionaryMainParam queryParam) {
        Page<Dictionary> page = new Page<>(queryParam.getPageNum(), queryParam.getPageSize());
        return dictionaryService.getPages(page, queryParam);
    }

    @ApiOperation(value = "数据字典-列表")
    @PostMapping(value = "/list")
    @JWTAuth
    public CommonResult<List<DictionaryDetailVO>> list(@RequestBody DictionaryListParam param) {
        return dictionaryService.getList(param);
    }

    @ApiOperation(value = "数据字典-详情")
    @GetMapping("/get/{id}")
    @ApiImplicitParam(paramType = "path", name = "id", value = "字典ID", required = true, dataType = "String")
    @JWTAuth
    public CommonResult<DictionaryDetailVO> detail(@PathVariable String id) {
        return dictionaryService.getDetail(id);
    }


    @ApiOperation(value = "数据字典-新增")
    @PostMapping(value = "/add")
    @JWTAuth
    @Transactional(rollbackFor = Exception.class)
    public CommonResult<String> add(@RequestBody DictionaryAddParam param, HttpServletRequest request) {
        InitUserDto initUserDto = initUser(request);
        CommonResult<String> add = dictionaryService.add(param, initUserDto);
        Long code = add.getCode();
        if (code.equals(ErrorCode.SUCCESS.getCode())){
            addSysLogUtil.addLog(initUserDto, LogTypeEnum.LOG_TYPE_ADD.getTypeNum(), LogModuleEnum.LOG_MODULE_DICTIONARY.getModuleNum(),"添加字典目录",param);
        }
        return add;
    }


    @ApiOperation(value = "数据字典-修改")
    @PostMapping(value = "/update")
    @JWTAuth
    @Transactional(rollbackFor = Exception.class)
    public CommonResult<String> update(@RequestBody DictionaryUpdateParam param, HttpServletRequest request) {
        InitUserDto initUserDto = initUser(request);
        CommonResult<String> update = dictionaryService.update(param, initUserDto);
        Long code = update.getCode();
        if (code.equals(ErrorCode.SUCCESS.getCode())){
            addSysLogUtil.addLog(initUserDto, LogTypeEnum.LOG_TYPE_UPDATE.getTypeNum(), LogModuleEnum.LOG_MODULE_DICTIONARY.getModuleNum(),"修改字典目录",param);
        }
        return update;
    }

    @ApiOperation(value = "数据字典-批量删除")
    @PostMapping(value = "/delete/{ids}")
    @JWTAuth
    @Transactional(rollbackFor = Exception.class)
    public CommonResult<String> delete(@PathVariable String ids, HttpServletRequest request) {
        InitUserDto initUserDto = initUser(request);
        CommonResult<String> delete = dictionaryService.delete(ids, initUserDto);
        Long code = delete.getCode();
        if (code.equals(ErrorCode.SUCCESS.getCode())){
            addSysLogUtil.addLog(initUserDto, LogTypeEnum.LOG_TYPE_DELETE.getTypeNum(), LogModuleEnum.LOG_MODULE_DICTIONARY.getModuleNum(),"删除字典目录",ids);
        }
        return delete;
    }

}
